/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controllers.FileAnalysis;
import java.io.File;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
 *
 * @author Jhovany Amaya
 */
public class LoadFilePane extends GridPane{
    
    private final Label instructionL;
    private final Label detailsL;
    private final Label nameFileL;
    private final Label nameFileValueL;
    private final Label pathFileL;
    private final Label pathFileValueL;
    private final Label readableL;
    private final Label readableValueL;
    private final Label sizeFileL;
    private final Label sizeFileValueL;
    private final Label numberOfLinesOfFileL;
    private static Label numberOfLinesOfFileValueL;
    private final Label lastModifDateOfFileL;
    private final Label lastModifDateOfFileValueL;
    private final Separator separator;
    private final Separator separator2;
    private final TextField pathTF;
    private final Button findButton;
    private final Button loadButton;
    private final FileChooser fileChooser;
    private File fileToLoad;
    private FileAnalysis analysisControl;
    
    private static long linesNum;
    
    public static long getNumLines(){
      return linesNum;
    }
    
    private final Application application;

    public LoadFilePane(final Stage primaryStage,final Application application) {
        this.application=application;
        setAlignment(Pos.CENTER);
        fileChooser = new FileChooser();
        setPadding(new Insets(60, 30, 20, 30));
        setHgap(12);
        setVgap(7);
        instructionL = new Label("Seleccione el archivo a cargar");
        detailsL = new Label("\tDetalles del archivo...");
        nameFileL = new Label("Nombre:");
        nameFileValueL = new Label("-");
        pathFileL = new Label("Ruta:");
        pathFileValueL = new Label("-");
        readableL = new Label("Permiso de lectura:");
        readableValueL = new Label("-");
        sizeFileL = new Label("Tamaño:");
        sizeFileValueL = new Label("-");
        numberOfLinesOfFileL = new Label("Número de líneas");
        numberOfLinesOfFileValueL = new Label("-");
        lastModifDateOfFileL = new Label("Fecha de última modificación");
        lastModifDateOfFileValueL = new Label("-");
        separator = new Separator();
        separator2 = new Separator();
        pathTF = new TextField();
        pathTF.setMinWidth(400);
        findButton = new Button("Examinar...");
        loadButton = new Button("Cargar");
        findButton.setOnAction(browseEvent(primaryStage));
        loadButton.setOnAction(loadFile());
        loadButton.setDisable(true);
        add(instructionL, 0, 1);
        add(pathTF, 1, 1);
        add(findButton, 2, 1);
        add(separator,0,2,3,1);
        add(detailsL,0,3,2,1);
        add(nameFileL,0,4); add(nameFileValueL,1,4);
        add(pathFileL,0,5); add(pathFileValueL,1,5);
        add(readableL,0,6); add(readableValueL,1,6);
        add(sizeFileL,0,7); add(sizeFileValueL,1,7);
        add(numberOfLinesOfFileL,0,8); add(numberOfLinesOfFileValueL,1,8);
        add(lastModifDateOfFileL, 0, 9); add(lastModifDateOfFileValueL,1,9);
        add(separator2,0,10,3,1);
        add(loadButton,2,11);
    }

    private EventHandler<ActionEvent> browseEvent(final Stage primaryStage) {
        return new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                // Mostrar GUI para cargar archivo
                fileToLoad = fileChooser.showOpenDialog(primaryStage);
                if (fileToLoad != null){
                    analysisControl = new FileAnalysis();
                    ArrayList<String> list = analysisControl.getFileDatails(fileToLoad.getPath());

                    pathTF.setText(list.get(1));
                    nameFileValueL.setText(list.get(0));
                    pathFileValueL.setText(list.get(1));
                    readableValueL.setText(list.get(2));
                    sizeFileValueL.setText(list.get(3));
                    linesNum=Long.parseLong(list.get(4));
                    numberOfLinesOfFileValueL.setText(linesNum + " líneas");
                    lastModifDateOfFileValueL.setText(list.get(5));
                    loadButton.setDisable(false);
                }else{
                    pathTF.setText("");
                    nameFileValueL.setText("-");
                    pathFileValueL.setText("-");
                    readableValueL.setText("-");
                    sizeFileValueL.setText("-");
                    numberOfLinesOfFileValueL.setText("-");
                    lastModifDateOfFileValueL.setText("-");
                    loadButton.setDisable(true);
                }
            }
        };
    }
    
    protected void enableLoad(){
        findButton.setDisable(false);
        pathTF.setEditable(true);
    }

    private EventHandler<ActionEvent> loadFile() {
        return new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                if (fileToLoad != null){
                    loadButton.setDisable(true);
                    pathTF.setEditable(false);
                    findButton.setDisable(true);
                    long numberOfLinesFile = Long.valueOf(numberOfLinesOfFileValueL.getText().replaceAll(" líneas", ""));
                    ((MainWindow)application).startFileAnalysis(fileToLoad.getAbsolutePath(),numberOfLinesFile);
                }
            }
        };
    }
}
